﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataLayer.Model.Objects;
using Overview.Components;
using BuisnessLayer;

namespace Overview
{
    /// <summary>
    /// Interaction logic for WorkProcessEditorControl.xaml
    /// </summary>
    public partial class WorkProcessEditorControl : Window
    {
        private Profile ProfileData;
        private WorkProcess WorkProcess;

        public WorkProcessEditorControl(ref Profile ProfileData)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.ProfileData = ProfileData;
            LoadAvailableMacroses();
            LoadWorkProcessData();
            LoadWorkProcessMacroses();
        }

        private void LoadWorkProcessMacroses()
        {
            if (WorkProcess == null || WorkProcess.Macroses ==null)
                return;

            foreach(var macro in WorkProcess.Macroses)
                AddItemToWorkProcessMacroses(macro, -1);
        }

        private void AddItemToWorkProcessMacroses(Macro pMacro, int pIndex)
        {
            var macroItem = new ActionInListComponent(pMacro);
            macroItem.EditMacroCall += new EventHandler(EditSelectedMacro);
            //macroItem.DragEnter += new DragEventHandler(Grid_DragEnter);
            //macroItem.Drop += new DragEventHandler(SpnlMacroActionsGrid_Drop);
            if (pIndex < 0)
                SpnlWorkProcessActions.Children.Add(macroItem);
            else
            {
                SpnlWorkProcessActions.Children.Insert(pIndex, macroItem);
            }
        }

        private void LoadWorkProcessData()
        {
            if (WorkProcess == null)
            {
                WorkProcess = new WorkProcess();
                ProfileData.ActiveProcess = WorkProcess;
                ProfileData.AddWorkProcess(WorkProcess);
                CbxMacroIsRepeatable.IsChecked = WorkProcess.Cyclic;
                if(WorkProcess.Name!= null)
                    LblMacroName.Content = WorkProcess.Name;
            }
        }

        private void LoadAvailableMacroses()
        {
            //TODO:  remove code duplication from here and macro edit control
            ExpnMacrosesGrid.RowDefinitions.Add(new RowDefinition());
            var columnIndex = 0;
            var rowIndex = 0;
            if(ProfileData.UserMacroses != null)
            foreach (var macro in ProfileData.UserMacroses)
            {
                var macroControl = new ActionControl(macro);
                macroControl.MouseDoubleClick += SelectMacroToEdit; 
                Grid.SetColumn(macroControl, columnIndex++);
                Grid.SetRow(macroControl, rowIndex);
                if (columnIndex > 3)
                {
                    columnIndex = 0;
                    rowIndex++;
                    ExpnMacrosesGrid.RowDefinitions.Add(new RowDefinition());
                }
                ExpnMacrosesGrid.Children.Add(macroControl);
            }

            //New macro button.
            var optionNew = new Button { 
                Width = 48,
                Height =48
            };
            optionNew.Click += NewMacroButton_Click;
            Grid.SetColumn(optionNew, columnIndex++);
            Grid.SetRow(optionNew, rowIndex);
            ExpnMacrosesGrid.Children.Add(optionNew);
        }

        private void NewMacroButton_Click(object sender, RoutedEventArgs e)
        {
            //TODO: ask user macro name or add loggic to add post name
            var macro = new Macro{Name = "New Macro"};
            this.ProfileData.AddUserMacroses(macro);
            ShowMacroEditDialog(macro);
        }

        private void SelectMacroToEdit(object sender, MouseButtonEventArgs e)
        {
            var macro = ((ActionControl)sender).Macro;
            ShowMacroEditDialog(macro);
        }

        private void ShowMacroEditDialog(Macro pMacro) {
            var macroeditor = new MacroEditorControl(ProfileData.Character, ref pMacro);
            if (macroeditor.ShowDialog() == true)
            {
                //Feedback obut macro changed
            }
            else
            {
                //Feedback about macro not changed
            } 
        }

        #region Event Handlers

        #region Custom Events

        private void EditSelectedMacro(object sender, EventArgs e)
        {
            var macro = sender as Macro;
            ShowMacroEditDialog(macro);
        }

        #endregion


        #region Button clicks

        private void CbxMacroIsRepeatable_Checked(object sender, RoutedEventArgs e)
        {
            WorkProcess.SetRepeat(((CheckBox)sender).IsChecked);
        }

        private void ExpnMacrosesGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }

        private void ExpnMacrosesGrid_PreviewMouseMove(object sender, MouseEventArgs e)
        {

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            //Get all childs wrom macro list
            //save to work process
            //add to profile 
            this.DialogResult = true;
            this.Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        #endregion

        private void LblMacroName_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var newName = UserInteract.AskStringData(new AskerMessage
            {
                ConfirmContent = "Change",
                Message = "Enter macro name.",
                Title = "Macro",
                DiscardContent = "Cancel"
            });
            if (newName == null)
                return;

            WorkProcess.Name = newName;
            LblMacroName.Content = WorkProcess.Name;
        }

        #endregion
    }
}
